<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
        case "add_form" :
                title_bar($langchargeratemaster);
                ?>
                <br><? echo $langoperation . " - " . $langadd?><br>
                <form method = "GET" action="chargeratemaster.php" name = "chargerate">
                <table>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langchargeid ?>
                                <? charge_id_selection_box() ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid ?>
                                <? company_id_selection_box(1) ?>
                                <td><? echo $langemptyall ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
                                <td><input type = 'text' name = 'ClientID'>
                                        <input type = 'button' value = 'S' onclick=opensearch('client','chargerate.ClientID')>
                                <td><? echo $langemptyall ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langjobid ?>
                                <td><input type = 'text' name = 'JobID'>
                                        <input type = 'button' value = 'S' onclick=opensearch('job','chargerate.JobID')>
                                <td><? echo $langemptyall ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langeffectivedate ?>
                                <td><input type = 'text' name = 'EffectiveDate' maxlength='10'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langchargerate ?>
                                <td><input type = 'text' name = 'ChargeRate' maxlength = '10'>
                                <input name="NetRate" type="checkbox" value="ON" checked> <? echo $langnetrate ?>
                        <!-- AG Change 20.11.2005 -->
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langtaxrate ?>
                                <td><input type = 'text' name = 'TaxRate' maxlength = '5'>
                                        <input type = 'hidden' name = 'action' value = 'add'>
                        <tr>
                                <td>
                                <td><input type = 'submit' value = '<? echo $langadd ?>'>
                </table>
                </form>

                <script>
                function opensearch(search_type, fieldname) {
                                url_text = 'search.php?master=' + search_type + '&inputname=' + fieldname;
                                window.open(url_text ,'','width=700,height=500');
                }
                </script>
                <?
                break;

        case "add" :
                // validate client ID
                if (! empty($_GET['ClientID'])) {
                        $sql = "SELECT ClientID FROM clientmaster WHERE Enable = 1 AND ClientID = '" . $_GET['ClientID'] . "'";
                        $sql_result = sql_execute($sql);

                        if (mysql_num_rows($sql_result) == 0) $error_text .= "<br>- $langclientid : $langnotexist<br>";
                }

                // validate job ID
                if (! empty($_GET['JobID'])) {
                        $sql = "SELECT JobID FROM jobmaster WHERE Enable = 1 AND JobID = '" . $_GET['JobID'] . "'";
                        $sql_result = sql_execute($sql);

                        if (mysql_num_rows($sql_result) == 0) $error_text .= "<br>- $langjobid : $langnotexist<br>";
                }

                // validate Charge Rate is a number and less than 2 decimal places
                if (! validate_number($_GET['ChargeRate'])) $error_text .= "<br> - $langinvalidnumber<br>";

                // AG Change 20.11.2005
                // validate Tax Rate is a number and less than 2 decimal places
                if (! validate_number($_GET['TaxRate'])) $error_text .= "<br> - $langinvalidnumber<br>";

                // convert user input effective date to format YYYYMMDD and validate Effective Date
                $date_effective = validate_date($_GET['EffectiveDate'], 1) ;
                if (! $date_effective) $error_text .= "<br>- $langinvaliddate<br>";

                // VAT handling
                $net_rate = 0;
                if ($_GET['NetRate']=="ON")
                    $net_rate = 1;

                if ($error_text) {
                        message_box($error_text);
                        echo "<script>history.go(-1)</script>";
                        exit;
                }

                // AG Change 20.11.2005
//              $sql = "INSERT INTO chargeratemaster (ChargeID, CompanyID, ClientID, JobID, EffectiveDate, ChargeRate) values (";
                $sql = "INSERT INTO chargeratemaster (ChargeID, CompanyID, ClientID, JobID, EffectiveDate, ChargeRate, TaxRate, NetRate) values (";
                $sql .= "'" . $_GET['ChargeID'] . "' ,";
                $sql .= "'" . $_GET['CompanyID'] . "' ,";
                $sql .= "'" . strtoupper($_GET['ClientID']) . "' ,";
                $sql .= "'" . strtoupper($_GET['JobID']) . "' ,";
                $sql .= "'$date_effective' ,";
                $sql .= $_GET['ChargeRate'] ;
                $sql .= ",".$_GET['TaxRate'] ;                                                       // AG Change 20.11.2005
                $sql .= ",".$net_rate ;
                $sql .= ")";

                $sql_result = sql_execute($sql);
                echo "<br>$langchargerate $langadded<br>";
                title_bar($langchargeratemaster,1);
                break;

        case "modify_form" :
                title_bar($langchargeratemaster, 1);
                // Obtain values from table
                $sql = "SELECT * FROM chargeratemaster WHERE ChargeID='" . $_GET['ChargeID'] . "' AND " .
                                "CompanyID='" . $_GET['CompanyID'] . "' AND " .
                                "ClientID='" . $_GET['ClientID'] . "' AND " .
                                "JobID='" . $_GET['JobID'] . "' AND " .
                                "EffectiveDate='" . $_GET['EffectiveDate'] . "'";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);

                $user_date = convert_mysql_date($_GET['EffectiveDate']);
                ?>

                <br><? echo $langoperation . " - " . $langmodify ?><br>
                <form method = "GET" action="chargeratemaster.php">
                <table>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langchargeid ?>
                                <td><input type = 'text' name = 'ChargeID' value = '<? echo $sql_data['ChargeID'] ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid ?>
                                <td><input type = 'text' name = 'CompanyID' value = '<? echo $sql_data['CompanyID'] ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
                                <td><input type = 'text' name = 'ClientID' value = '<? echo $sql_data['ClientID'] ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langjobid ?>
                                <td><input type = 'text' name = 'JobID' value = '<? echo $sql_data['JobID'] ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langeffectivedate ?>
                                <td><input type = 'text' name = 'EffectiveDate' value = '<? echo $user_date ?>'
                                        maxlength = '10' readonly>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langchargerate ?>
                                <td><input type = 'text' name = 'ChargeRate' maxlength = '10' value='<?
                                        echo $sql_data['ChargeRate'] ?> '>
                                <input name="NetRate" type="checkbox" value="ON"
                                <?
                                if (($sql_data['NetRate']) && ($sql_data['NetRate']==1)) echo "checked";
                                else if (($sql_data['NetRate']) && ($sql_data['NetRate']==0)) echo "";
                                else echo "checked";
                                ?>
                                > <? echo $langnetrate ?>
                        <!-- AG Change 20.11.2005 -->
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langtaxrate ?>
                                <td><input type = 'text' name = 'TaxRate' maxlength = '5' value='<?
                                        echo $sql_data['TaxRate'] ?> '>


                       <tr><td>
                                <input type = 'hidden' name = 'action' value = 'modify'>
                                <td><input type = 'submit' value = '<? echo $langmodify ?>'>
                </table>
                </form>
                <?
                break;

        case "modify" :
                $date_effective = validate_date($_GET['EffectiveDate'],1);

                // VAT handling
                $net_rate = 0;
                if ($_GET['NetRate']=="ON")
                    $net_rate = 1;

                $sql = "UPDATE chargeratemaster SET ChargeRate=" . $_GET['ChargeRate'] .
                                                  ",TaxRate=" . $_GET['TaxRate'] .              // AG Change 20.11.2005
                                                  ",NetRate=" . $net_rate .                     // AG Change 26.12.2005
                                " WHERE " .
                                "ChargeID = '" . $_GET['ChargeID'] . "' AND " .
                                "CompanyID = '" . $_GET['CompanyID'] . "' AND " .
                                "ClientID = '" . strtoupper($_GET['ClientID']) . "' AND " .
                                "JobID = '" . strtoupper($_GET['JobID']) . "' AND " .
                                "EffectiveDate = '" . $date_effective . "'";

                $sql_result = sql_execute($sql);
                echo "<br>$langchargerate $langmodified<br>";
                title_bar($langchargeratemaster, 1);
                break;

        case "delete" :
                $sql = "SELECT ChargeID FROM billtransaction WHERE ChargeID = '" . $_GET['ChargeID'] ."'";

                if (! empty($_GET['CompanyID'])) $sql .= " AND CompanyID = '" . $_GET['CompanyID']. "'";
                if (! empty($_GET['ClientID'])) $sql .= " AND ClientID = '" . $_GET['ClientID']. "'";
                if (! empty($_GET['JobD'])) $sql .= " AND JobID = '" . $_GET['JobID']. "'";

                $sql .= " AND TransactionDate >= '" . $_GET['EffectiveDate'] . "'";
                if ($_GET['ToDate']) $sql .= " AND TransactionDate < '" . $_GET['ToDate'] . "'";

                $sql .= " LIMIT 1";
                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                if ($sql_data[0]) {
                        $message = "<br>" . $langtransactionexist . "<br>" . $langcannotdelrate . "<br>";
                        message_box($message);
                        ?>
                                <script>history.go(-1);</script>
                        <?
                        exit;
                }

                $sql = "DELETE FROM chargeratemaster WHERE " .
                           "ChargeID = '" . $_GET['ChargeID'] . "' AND " .
                           "CompanyID = '" . $_GET['CompanyID'] . "' AND " .
                           "ClientID = '" . $_GET['ClientID'] . "' AND " .
                           "JobID = '" . $_GET['JobID'] . "' AND " .
                           "EffectiveDate='" . $_GET['EffectiveDate'] . "'";
                $sql_result = sql_execute($sql);
                echo "<br>$langchargerate $langdeleted<br>";
                title_bar($langchargeratemaster, 1);
                break;

        case "count" :
                // validate Effective Date
                if (! empty($_GET['from_EffectiveDate'])) {
                        // convert user input effective date to format YYYYMMDD
                        $date_effective_from = validate_date($_GET['from_EffectiveDate'], 1) ;
                        if (! $date_effective_from) {
                                ?>
                                <script>
                                        alert('<? echo $langinvaliddate . " : " . $langfrom ?>');
                                        history.go(-1);
                                </script>
                                <?
                                exit;
                        }
                }

                if (! empty($_GET['to_EffectiveDate'])) {
                        // convert user input effective date to format YYYYMMDD
                        $date_effective_to = validate_date($_GET['to_EffectiveDate'], 1) ;
                        if (! $date_effective_to) {
                                ?>
                                <script>
                                        alert('<? echo $langinvaliddate . " : " . $langto ?>');
                                        history.go(-1);
                                </script>
                                <?
                                exit;
                        }
                }

                save_search_list();
                $url_text = retrieve_search_list();
                $sql = select_statement('chargeratemaster');

                $sql_result = sql_execute($sql);
                $sql_data = mysql_fetch_array($sql_result);
                $no_of_record = $sql_data[0];

                ?>
                <script>
                this.location = 'chargeratemaster.php?action=browse&total_record=<? echo $no_of_record ?>
                                                &record_offset=0<? echo $url_text ?>'
                </script>
                <?
                break;

        case "browse" :
                navigation_bar($langchargeratemaster,1);
                column_headings(array($langchargeid, $langcompanyid, $langclientid, $langjobid, $langeffectivedate,
                        $langchargerate,$langnetrate,$langtaxrate));                         // AG Change 20.11.2005

                // List ChargeRates here
                // $sql = select_statement('chargeratemaster',1);
                $sql = "SELECT * FROM chargeratemaster ";
                $sql .= " ORDER BY ChargeID, CompanyID, ClientID, JobID, EffectiveDate DESC";
                $sql .= " LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
                $sql_result = sql_execute($sql);

                $last_effective_date = '';
                $last_charge_rate_pattern = '';

                //die(mysql_fetch_array($sql_result));

                while ($sql_data = mysql_fetch_array($sql_result)) {
                        $user_date = convert_mysql_date($sql_data['EffectiveDate']);
                        $rowcolor = row_color_change();
                        echo "\n<tr bgcolor='$rowcolor'>";
                        echo "<td>";
                        echo "<a href = 'chargeratemaster.php?action=modify_form&ChargeID=".$sql_data['ChargeID'] .
                                 "&CompanyID=" . $sql_data['CompanyID'] .
                                 "&JobID=" . $sql_data['JobID'] .
                                 "&ClientID=" . $sql_data['ClientID'] .
                                 "&EffectiveDate=" . $sql_data['EffectiveDate'] .
                                 "'>" . $sql_data['ChargeID'] . "</a>";
                        echo "<td>" . $sql_data['CompanyID'];
                        echo "<td>" . $sql_data['ClientID'];
                        echo "<td>" . $sql_data['JobID'];

                        echo "<td align='right'>";
                        echo substr($user_date,0,2) . "-" . substr($user_date,2,2) . "-" . substr($user_date,4,4);
                        echo "<td align='right'>";                              // AG Change 20.11.2005
                        echo $sql_data['ChargeRate'];                           // AG Change 20.11.2005
                        echo "<td align='right'>";                              // AG Change 26.12.2005
                        echo $sql_data['NetRate'];                              // AG Change 26.12.2005
                        echo "<td align='right'>";
                        echo $sql_data['TaxRate'];
                        echo "<td>";
                        echo "<a href = 'chargeratemaster.php?action=delete&ChargeID=" . $sql_data['ChargeID'] .
                             "&CompanyID=" . $sql_data['CompanyID'] .
                             "&JobID=" . $sql_data['JobID'] .
                             "&ClientID=" . $sql_data['ClientID'] .
                             "&EffectiveDate=" . $sql_data['EffectiveDate'];
                        $charge_rate_pattern = $sql_data['ChargeID'] . $sql_data['CompanyID'] .
                                                                   $sql_data['ClientID'] . $sql_data['JobID'];
                        if ($last_charge_rate_pattern == $charge_rate_pattern) {
                                echo "&ToDate=" . $last_effective_date;
                        }
                        echo "'>$langdelete</a>";
                        $last_effective_date = $sql_data['EffectiveDate'];
                        $last_charge_rate_pattern = $charge_rate_pattern;
                }
                echo "</table>";
                break;

        default :
                title_bar($langchargeratemaster, 1);
                $field[] = array('title'=>$langchargeid, 'field'=>'ChargeID', 'length'=>10);
                $field[] = array('title'=>$langcompanyid, 'field'=>'CompanyID', 'length'=>10);
                $field[] = array('title'=>$langclientid, 'field'=>'ClientID', 'length'=>10);
                $field[] = array('title'=>$langjobid, 'field'=>'JobID', 'length'=>10);
                $field[] = array('title'=>$langeffectivedate, 'field'=>'EffectiveDate', 'length'=>10);
                search_form($field, $_SERVER['PHP_SELF']);
                break;
}

html_footer();
?>
